package com.his.struts.dao.cook;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;

import org.apache.log4j.Logger;

import com.his.struts.dao.comment.CommentImp;
import com.his.struts.form.CookForm;
import com.his.struts.util.DbUtil;

/**
 * 实现食谱接口类
 * @author mlxnle
 *
 */
public class CookImp implements CookDao{
	Logger log = Logger.getLogger(CookImp.class);
	DbUtil db;
	ResultSet rs = null;
	
	/**
	 * 获得食谱列表
	 * @return ArrayList<CookForm>
	 */
	public ArrayList<CookForm> getCookList() {
		db = new DbUtil();
		ArrayList<CookForm> cookList = new ArrayList<CookForm>();
		// 数据库得到单月食谱
		// 查出当天是第几天，获得当天食谱，添加到集合最后
		Calendar cal = Calendar.getInstance();
		int month = cal.get(Calendar.DAY_OF_MONTH);
		// 得到本月天数
		int countday = getCurrentMonthLastDay();
		String sql = "select * from foodcook order by cid asc limit ?";
		ArrayList list = new ArrayList();
		list.add(countday);
		rs = db.executeQuery(sql, list);
		try {
			while (rs.next()) {
				CookForm cookForm = new CookForm();
				cookForm.setCid(rs.getInt(1));
				cookForm.setCookname(rs.getString(2));
				cookForm.setSmallurl(rs.getString(3));
				cookList.add(cookForm);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		// 将当日食谱添加到集合最后
		cookList.add(cookList.get(month - 1));
		return cookList;
	}

	/**
	 * 获得所有食谱
	 * @return ArrayList<CookForm>
	 */
	public ArrayList<CookForm> getAllCook(ResultSet rs) {
		db = new DbUtil();
		ArrayList<CookForm> cookList = new ArrayList<CookForm>();
		try {
			while (rs.next()) {
				CookForm cookForm = new CookForm();
				cookForm.setCid(rs.getInt(1));
				cookForm.setCookname(rs.getString(2));
				cookForm.setSmallurl(rs.getString(3));
				cookList.add(cookForm);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return cookList;
	}

	/**
	 * 添加一个食谱
	 * @param cookForm
	 * @return boolean
	 */
	public boolean addCook(CookForm cookForm) {
		db = new DbUtil();
		Boolean ok = false;
		String sql = "insert into foodcook  (cookname,smallurl) values(?,?)";
		ArrayList list = new ArrayList();
		list.add(cookForm.getCookname());
		list.add(cookForm.getSmallurl());
		int line = db.executeUpdate(sql, list);
		if (line != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}
	

	/**
	 * 删除一个食谱
	 * @param cid
	 * @return boolean
	 */
	public boolean deleteCook(int cid) {
		db = new DbUtil();
		boolean ok = false;
		String sql = "delete from foodcook where cid =?";
		ArrayList list = new ArrayList();
		list.add(cid);
		int line = db.executeUpdate(sql, list);
		if (line != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}
	
	/**
	 * 更新食谱
	 * @param whatis 要更新的字段
	 * @param img 对应内容
	 * @param cid 食谱id
	 * @return boolean
	 */
	public boolean updateCook(String whatis, String img, int cid) {
		db = new DbUtil();
		boolean ok = false;
		String sql = "update foodcook set ? = ? where cid = ?";
		ArrayList list = new ArrayList();
		list.add(whatis);
		list.add(img);
		list.add(cid);
		int line = db.executeUpdate(sql, null);
		if (line != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}

	/**
	 * 获得当月天数
	 * 
	 * @return
	 */
	public static int getCurrentMonthLastDay() {
		Calendar a = Calendar.getInstance();
		a.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		a.roll(Calendar.DATE, -1);// 日期回滚一天，也就是最后一天
		int maxDate = a.get(Calendar.DATE);
		return maxDate;
	}

}
